﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Golden_Miner
{
    class Hook
    {
        public Rectangle Rectangle { get; set; }

        /// <summary>
        /// 绳子长度
        /// </summary>
        public int length { get; set; }
        
        /// <summary>
        /// 伸展速度
        /// </summary>

        private int strechspeed { get; set; }
        private int tempspeed { get; set; }

        /// <summary>
        /// 旋转速度
        /// </summary>
        private int speed { get; set; }

        /// <summary>
        /// 旋转角度
        /// </summary>
        public int angle { get; set; }

        /// <summary>
        /// 是否旋转
        /// </summary>
        private bool iscircle { get; set; }

        /// <summary>
        /// 旋转中心坐标
        /// </summary>
        /// <param name="rec"></param>
        private Point central { get; set; }
       
        public Hook(Rectangle rec)
        {
            angle = 0;
            speed = 10;
            length = 10;
            strechspeed = 40;
            tempspeed = 20;
            iscircle = true;
            Rectangle = rec;
            central = new Point (465, 90);
        }


        public void Draw(Graphics g)
        {
            g.TranslateTransform(central.X,central .Y);
            g.RotateTransform(angle - 90);
            g.DrawLine(new Pen(new SolidBrush(Color.Black ), 1), new Point(0, 0), new Point(0, length));
            g.DrawImage(Properties.Resources.hook, new Rectangle(-18, length, 35, 35));


        }

        public void Tick()
        {
            if (iscircle)
            {
                //旋转状态
                angle += speed;
                if (angle <= 0)
                {
                    angle = 0;
                    speed *= -1;
                }
                if (angle >= 180)
                {
                    angle = 180;
                    speed *= -1;
                }

            }
            else
            {
                //伸展状态
                length += strechspeed;
                if (length >= 700)
                {
                    length = 700;
                    strechspeed *= -1;
                }
                if (length <= 10)
                {
                    length = 10;
                    strechspeed =40;
                    iscircle = true;
                }
            }

        }

        /// <summary>
        /// 处理钩子向下
        /// </summary>
        /// <param name="key"></param>
        public void KeyDown(Keys key)
        {
            if (key == Keys.S && iscircle)
            {
                iscircle = false;
            }
        }

        /// <summary>
        /// 当发生碰撞时钩子收回
        /// </summary>
        public void hookBack(int oresize)
        {
            switch (oresize )
            {
                case 1:
                    strechspeed = -20;
                    break;
                case 2:
                    strechspeed = -7;
                    break;
                case 3:
                    strechspeed = -3;
                    break;
            }
            
        }

        /// <summary>
        /// 求绳子末端在原坐标系的坐标
        /// </summary>
        /// <param name="length"></param>
        /// <param name="angle"></param>
        /// <returns></returns>
        public Point rePoint(int length,int angle)
        {
            double tempx = length * Math.Cos(angle*Math .PI /180);
            double tempy = length * Math.Sin(angle * Math.PI / 180);
            return new Point((int)tempx+central.X, (int)tempy+central.Y);
        }
    }
}
